/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TesteLimpar.java
 *
 * Created on 22/07/2009, 06:46:52
 */

package gui;

import facade.AdvogadoFacade;
import java.util.Calendar;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import modelo.Cliente;

/**
 *
 * @author andrehadm
 */
public class TesteLimpar extends javax.swing.JDialog {

    AdvogadoFacade advogadoFacade;

    /** Creates new form TesteLimpar */
    public TesteLimpar(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        advogadoFacade = new AdvogadoFacade();

        



        jComboBox1.addItem("Andre");
        jComboBox1.addItem("Anderson");
        jComboBox1.addItem("Antonio");
        jComboBox1.addItem("Tomas");
        jComboBox1.addItem("Vinicius");
        jComboBox1.setKeySelectionManager( new JComboBox.KeySelectionManager() {
             StringBuffer buffer = new StringBuffer();
             long tempo;
             public int selectionForKey(char aKey, ComboBoxModel aModel) {
                 System.out.println("tempo : " + tempo);
                 System.out.println("tempo 2 : " + Calendar.getInstance().getTimeInMillis());
                 if (Calendar.getInstance().getTimeInMillis() - tempo > 500) {
                     buffer.delete(0, buffer.length());
                 }
                 buffer.append(aKey);
                 tempo = Calendar.getInstance().getTimeInMillis();
                 int size = aModel.getSize();
                 System.out.println(buffer.toString());
                 for (int i = 0; i < size; i++) {
                     if (aModel.getElementAt(i).toString().toUpperCase().startsWith(buffer.toString().toUpperCase())) {
                         return i;
                     }
                 }
                 return 0;
             }
         });
         carregaClientes();
         carregaClientes2();
    }


    private void carregaClientes () {
        DefaultListModel dlmC = new DefaultListModel();
        jList1 = new JList(dlmC);
        System.out.println("carreguei");
         
        for (Cliente cliente : advogadoFacade.getTodosClientes()) {
            dlmC.addElement(cliente);
            System.out.println("Cliente : "+ cliente.getId() + " - " + cliente.getNome());
        }

        
        jList1.setVisible(true);
        jScrollPane1.setViewportView(jList1);
//        jList1.revalidate();
    }


    private void carregaClientes2() {
        //jComboBox2 = new JComboBox();
        System.out.println("Tamanho da lista de clientes antes:" + jComboBox2.getItemCount());
        for (Cliente cliente : advogadoFacade.getTodosClientes()) {
            jComboBox2.addItem(cliente);
            System.out.println("Cliente : "+ cliente.getId() + " - " + cliente.getNome());
        }
        System.out.println("Tamanho da lista de clientes depois:" + jComboBox2.getItemCount());
        System.out.println("carreguei o segundo cliente");
        //jComboBox2.repaint();
        //jComboBox2.revalidate();
        //jComboBox2.setVisible(true);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jComboBox2 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jLabel1.setText("Pesquisar Cliente");

        jButton1.setText("Limpar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, 318, Short.MAX_VALUE))
                        .addGap(58, 58, 58))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jComboBox2, 0, 318, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        jTextField1.setText("");
        //jTextField1.setFocusable(true);
    }//GEN-LAST:event_jButton1MouseClicked

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        //jComboBox1.setKeySelectionManager(mk);
        atualizaComboBox();
    }//GEN-LAST:event_jTextField1KeyTyped

    private void atualizaComboBox(){
         
        
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TesteLimpar dialog = new TesteLimpar(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
